/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package controllers

import (
	"time"
	"fmt"
	//"strconv"
	"strings"
	//"time"

	"github.com/astaxie/beego"
	//"WebAdminSys/libs"
	"WebAdminSys/models"
)

type ChartDatasController struct {
	BaseController
}

func (self *ChartDatasController) RechargeList() {
	self.Data["pageTitle"] = "充值统计"
	//self.Ctx.WriteString("MangeAdminController.List")
	self.AddOperationLog("进入【充值统计】页面",0)
	self.display()
}

func (self *ChartDatasController) Add() {
	
}

func (self *ChartDatasController) Edit() {
	
}

func (self *ChartDatasController) AjaxSave() {

}

func (self *ChartDatasController) AjaxDel() {

	
}


func (self *ChartDatasController) RechargeTable() {

	fmt.Println("====================ChartDatasController RechargeTable======================")
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit


	date1:= strings.TrimSpace(self.GetString("date1"));
	date2 := strings.TrimSpace(self.GetString("date2"));
	selectUserID,err := self.GetInt("selectUserID");
	currDay := self.GetString("curDay");
	if err != nil {
		selectUserID = 1; 
	} 
	
	if currDay !="" {
		t:=time.Now().Unix();
		
		t2 := t + 24*3600 *1;//当天
		fmt.Println(t,t2);
		date1 = beego.UnixTimeToString2(t,"");
		date2 = beego.UnixTimeToString2(t2,"");
	}
	if date1 == "" && date2 == "" {
		

	}else{
		if date1 == "" {
			date1 = "2006-01-02"
		}
	
		if date2 == "" {
			date2 = "2019-01-02"
		}
	}
	
	//列表
	var uT1,uT2 int64 = 0,0;

	if date1 != "" && date2 !="" {
		uT1 = beego.StringTimeToUnix(date1,"2006-01-02");
		uT2 = beego.StringTimeToUnix(date2,"2006-01-02");
	}
	
	fmt.Println("page",page,"limit",limit,"date1:",date1,uT1,"date2:",date2,uT2,"selectUserID:",selectUserID);
	fmt.Println("=====================================================================")
	//查询条件
	filters := make([]interface{}, 0)
	filters = append(filters, "PLATFORM", selectUserID)
	result, count := models.UserPurchaseInfoGetList(page, self.pageSize,uT1,uT2, filters...)
	list := make(map[string]map[string]interface{}, len(result))
	for _, v := range result {
		 key := beego.UnixTimeToString2(v.PURCHASETIME,"");
		_,ok := list[key];
		if !ok {
			list[key] = make(map[string] interface{});
			list[key]["totalamount"] = 0;
			list[key]["iosamount"] = 0;
			list[key]["gmamount"] = 0;
			list[key]["peopleCount"] = make(map[int]int);
			list[key]["peopleCount2"] = 0;
			list[key]["rechargecount"] = 0;
			list[key]["date"] = beego.UnixTimeToString(v.PURCHASETIME,"");
			list[key]["date2"] = beego.UnixTimeToString2(v.PURCHASETIME,"")
		}
		row := list[key];
		//总充值金额
		row["totalamount"] = row["totalamount"].(int) + v.RMB;
		//IOS充值金额
		if v.PLATFORM ==1 {
			row["iosamount"] = row["iosamount"].(int) + v.RMB;
		}
		//GM充值金额
		row["gmamount"] = 0;
		
		//IOS充值人次
		mp  := row["peopleCount"].(map[int]int);
		mp[v.USERID] = v.USERID;
		row["peopleCount2"] = len(mp);
		//IOS充值次数
		row["rechargecount"] = row["rechargecount"].(int) +1;

	}

/* 	list2 := make([]map[string]interface{}, len(list))
	for k, v := range result {
		list2
	} */

	count = int64(len(list))
	self.ajaxList("成功", MSG_OK,count , list)
}


/**********************************************
** @Des: 在线统计
** @Author: qyj
** @Date:   2017-09-16 14:17:37
** @Last Modified by:   qyj
** @Last Modified time: 2017-09-17 11:14:07
***********************************************/

func (self *ChartDatasController) Onlinestatistics() {
	fmt.Println("====================ChartDatasController onlinestatistics======================")
	self.Data["pageTitle"] = "在线统计"
	//self.Ctx.WriteString("MangeAdminController.List")
	self.AddOperationLog("进入【在线统计】页面",0)
	self.display()
}


//获取当前在线数据
func (self* ChartDatasController) UserOnlineCurCount(){

	fmt.Println("====================ChartDatasController UserOnlineCurCount======================")
	date1:= strings.TrimSpace(self.GetString("date1"));
	date2 := strings.TrimSpace(self.GetString("date2"));

	platform,_ := self.GetInt("platform");

	fmt.Println(date1,date2,platform);


	result, count := models.UserOnlineInfoGetList(1,1,1);

	fmt.Println(result,count);

	self.ajaxList("成功", MSG_OK,count , result)
}

//获取统计表的数据
func (self *ChartDatasController) UserOnlineTable() {
	
		fmt.Println("====================ChartDatasController UserOnlineTable======================")
	
	
		date1:= strings.TrimSpace(self.GetString("date1"));
		date2 := strings.TrimSpace(self.GetString("date2"));
		platform,err := self.GetInt("platform");
		btnType ,err := self.GetInt("btnType");

		if err != nil {
			platform = 1; 
		} 
		
		if date1 == "" && date2 == "" {
			t:=time.Now().Unix();
			t2 := t + 24*3600 *1;//当天
		
			date1 = beego.UnixTimeToString2(t,"") +" 00";
			date2 = beego.UnixTimeToString2(t2,"")+" 00";
		}else{
			if date1 == "" {
				date1 = "2006-01-02 00"
			}
		
			if date2 == "" {
				date2 = "2019-01-02 00"
			}
		}
		
		//列表
		var uT1,uT2 int64 = 0,0;
	
		if date1 != "" && date2 !="" {
			uT1 = beego.StringTimeToUnix(date1,"2006-01-02 03");
			uT2 = beego.StringTimeToUnix(date2,"2006-01-02 03");
		}
		
		fmt.Println("date1:",date1,uT1,"date2:",date2,uT2,"platform:",platform,"btnType:",btnType);
		fmt.Println("=====================================================================")

		//查询
		result, count := models.AllUserOnlineCountGetList(btnType, platform,uT1,uT2)
				
	
		self.ajaxList("成功", MSG_OK,count , result)
	}


	//获取gameinfo数据
	func (self *ChartDatasController) GameInfoList() {
		fmt.Println("====================ChartDatasController GameInfoList======================")
		//列表
		page, err := self.GetInt("page")
		if err != nil {
			page = 1
		}
		limit, err := self.GetInt("limit")
		if err != nil {
			limit = 100
		}
		self.pageSize = limit

		filters := make([]interface{}, 0)

		result, count := models.GameInfoGetList(page, self.pageSize, filters...)

		//list := make([]map[string]interface{}, len(result))
		
		self.ajaxList("成功", MSG_OK,count , result)
	}
	
/**********************************************
** @Des: 日注册量统计
** @Author: qyj
** @Date:   2017-09-16 14:17:37
** @Last Modified by:   qyj
** @Last Modified time: 2017-09-17 11:14:07
***********************************************/
func (self *ChartDatasController) Registerstatistics() {
	fmt.Println("====================ChartDatasController Registerstatistics======================")
	self.Data["pageTitle"] = "日注册量统计"
	//self.Ctx.WriteString("MangeAdminController.List")
	self.AddOperationLog("进入【日注册量统计】页面",0)
	self.display()
}
	
//日注册量统计
func (self* ChartDatasController)RegisterCounList(){
	
		fmt.Println("====================ChartDatasController RegisterCounList======================")
		date1:= strings.TrimSpace(self.GetString("date1"));
		date2 := strings.TrimSpace(self.GetString("date2"));
		platform,err := self.GetInt("platform");
		if err != nil{
			platform = 0;
		}

		if date1 == "" && date2 == "" {
			t:=time.Now().Unix();
			t2 := t + 24*3600 *1;//当天
		
			date1 = beego.UnixTimeToString2(t,"");
			date2 = beego.UnixTimeToString2(t2,"");
		}else{
			if date1 == "" {
				date1 = "2006-01-02"
			}
		
			if date2 == "" {
				date2 = "2019-01-02"
			}
		}
		
		//列表
		var uT1,uT2 int64 = 0,0;
	
		if date1 != "" && date2 !="" {
			uT1 = beego.StringTimeToUnix(date1,"2006-01-02");
			uT2 = beego.StringTimeToUnix(date2,"2006-01-02");
			
		}

		
		fmt.Println(date1,date2,platform);
		uT2 = uT2 + 24*3600-1;  //传入时间为0点的 加24小时才会包括今天
		fmt.Println(date1,date2,uT1,platform);
		result, count := models.RegisterCountDaily(uT1,uT2,platform);
	
		fmt.Println(result,count);
	
		self.ajaxList("成功", MSG_OK,int64(count) , result)
	}

/**********************************************
** @Des: 日登录统量计
** @Author: qyj
** @Date:   2017-09-16 14:17:37
** @Last Modified by:   qyj
** @Last Modified time: 2017-09-17 11:14:07
***********************************************/
//
func (self *ChartDatasController) LoginStatistics() {

		fmt.Println("================== ChartDatasController LoginStatistics =======================");
		self.Data["pageTitle"] = "日登陆统计量"
	
		self.display()
		fmt.Println(self.TplName)
}

//日登陆量统计
func (self* ChartDatasController)LoginStatisticsCounList(){
	
		fmt.Println("====================ChartDatasController LoginStatisticsCounList======================")


		page, err := self.GetInt("page")
		
		if err != nil {
		
			page = 0
		}
		limit, err := self.GetInt("limit")
		if err != nil {
			limit = 100
		}
		self.pageSize = limit


		date1 := strings.TrimSpace(self.GetString("date1"));
		date2 := strings.TrimSpace(self.GetString("date2"));
		platform,err := self.GetInt("platform");
		if err != nil{
			platform = 0;
		}

		if date1 == "" && date2 == "" {
			t:=time.Now().Unix();
			t2 := t + 24*3600 *1;//当天
		
			date1 = beego.UnixTimeToString2(t,"");
			date2 = beego.UnixTimeToString2(t2,"");
		}else{
			if date1 == "" {
				date1 = "2006-01-02"
			}
		
			if date2 == "" {
				date2 = "2019-01-02"
			}
		}
		
		//列表
		var uT1,uT2 int64 = 0,0;
	
		if date1 != "" && date2 !="" {
			uT1 = beego.StringTimeToUnix(date1,"2006-01-02");
			uT2 = beego.StringTimeToUnix(date2,"2006-01-02");
		}

		uT2 = uT2 + 24*3600;  //传入时间为0点的 加24小时才会包括今天
		fmt.Println(page,limit,  date1,date2,uT1,platform);
		result, count := models.LoginCountDaily(page,self.pageSize,uT1,uT2,platform);
	
		fmt.Println(result,count);
	
		self.ajaxList("成功", MSG_OK,int64(count) , result)
}

/**********************************************
** @Des: 用户活跃统计
** @Author: qyj
** @Date:   2017-09-16 14:17:37
** @Last Modified by:   qyj
** @Last Modified time: 2017-09-17 11:14:07
***********************************************/
//
func (self *ChartDatasController) UserActiveStatistics() {
	
			fmt.Println("================== ChartDatasController UserActiveStatistics =======================");
			self.Data["pageTitle"] = "用户活跃统计"
			self.AddOperationLog("进入【用户活跃统计】页面",0)
			self.display()
			fmt.Println(self.TplName)
	}
	
	//活跃统计
	func (self* ChartDatasController)UserActiveStatisticsCounlist(){
		
			fmt.Println("====================ChartDatasController UserActiveStatisticsCounlist======================")
			
			btnType,err:= self.GetInt("btnType");
			if err != nil {
				btnType = 0;
			}
		

			page, err := self.GetInt("page")
			//fmt.Println("没去的覅了吗",page)
			if err != nil {
				//fmt.Println("没去的覅了吗")
				page = 0
			}
			limit, err := self.GetInt("limit")
			if err != nil {
				limit = 100
			}
			self.pageSize = limit
	
	
			date1 := strings.TrimSpace(self.GetString("date1"));
			date2 := strings.TrimSpace(self.GetString("date2"));
			platform,err := self.GetInt("platform");
			if err != nil{
				platform = 0;
			}
	
			if date1 == "" && date2 == "" {
				t:=time.Now().Unix();
				t2 := t + 24*3600 *1;//当天
			
				date1 = beego.UnixTimeToString2(t,"");
				date2 = beego.UnixTimeToString2(t2,"");
			}else{
				if date1 == "" {
					date1 = "2006-01-02"
				}
			
				if date2 == "" {
					date2 = "2019-01-02"
				}
			}
			
			//列表
			var uT1,uT2 int64 = 0,0;
		
			if date1 != "" && date2 !="" {
				uT1 = beego.StringTimeToUnix(date1,"2006-01-02");
				uT2 = beego.StringTimeToUnix(date2,"2006-01-02");
			}
	
			uT2 = uT2 + 24*3600-1;  //传入时间为0点的 加24小时才会包括今天
			fmt.Println(page,limit,  date1,date2,uT1,platform);
			result, count := models.ActiveCount(page,self.pageSize,uT1,uT2,platform,btnType);
		
			fmt.Println(result,count);
		
			self.ajaxList("成功", MSG_OK,int64(count) , result)
	}
	
/**********************************************
** @Des: 用户留存统计
** @Author: qyj
** @Date:   2017-09-16 14:17:37
** @Last Modified by:   qyj
** @Last Modified time: 2017-09-17 11:14:07
***********************************************/
//
func (self *ChartDatasController) UserRetentionStatistics() {
	
			fmt.Println("================== ChartDatasController UserRetentionStatistics =======================");
			self.Data["pageTitle"] = "用户留存统计"
			self.AddOperationLog("进入【用户活跃统计】页面",0)
			self.display()
			fmt.Println(self.TplName)
	}
	
	//存留统计
	func (self* ChartDatasController)Userretentioncountlist(){
		
			fmt.Println("====================ChartDatasController UserRetentionStatisticsCounlist======================")
			
			page, err := self.GetInt("page")
		
			if err != nil {
				page = 0
			}
			limit, err := self.GetInt("limit")
			if err != nil {
				limit = 100
			}
			self.pageSize = limit
	
	
			date1 := strings.TrimSpace(self.GetString("date1"));
			date2 := strings.TrimSpace(self.GetString("date2"));
			platform,err := self.GetInt("platform");
			if err != nil{
				platform = 0;
			}
	
			if date1 == "" && date2 == "" {
				t:=time.Now().Unix();
				t2 := t + 24*3600 *1;//当天
			
				date1 = beego.UnixTimeToString2(t,"");
				date2 = beego.UnixTimeToString2(t2,"");
			}else{
				if date1 == "" {
					date1 = "2006-01-02"
				}
			
				if date2 == "" {
					date2 = "2019-01-02"
				}
			}
			
			//列表
			var uT1,uT2 int64 = 0,0;
		
			if date1 != "" && date2 !="" {
				uT1 = beego.StringTimeToUnix(date1,"2006-01-02");
				uT2 = beego.StringTimeToUnix(date2,"2006-01-02");
			}
	
			uT2 = uT2 + 24*3600;  //传入时间为0点的 加24小时才会包括今天
			fmt.Println(page,limit,  date1,date2,uT1,platform);
			result, count := models.UserRetentionCount(page,self.pageSize,uT1,uT2,platform);
		
			fmt.Println(result,count);
		
			self.ajaxList("成功", MSG_OK,int64(count) , result)
	}
	

